﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Physis.Core.Insurer {

    public class ProgramType : CoreConfigurationObject {
        
        #region Private Properties

        private String code = String.Empty;

        #endregion 


        #region Public Properties

        public String Code { get { return code; } set { code = Data.Functions.SetValueMaxLength (value, Data.DataTypeConstants.BusinessCode3); } }

        #endregion 

        
        #region Constructors

        public ProgramType (Application forApplication) : base (forApplication) {

            return;

        }

        public ProgramType (Application forApplication, Int64 forId) : base (forApplication, forId) {

            return;

        }

        public ProgramType (Application forApplication, String forName) : base (forApplication, forName) {

            return;

        }

        #endregion


        #region Database Functions

        protected override Dictionary<String, String> Validate () {

            Dictionary<String, String> validationErrors = base.Validate ();


            if (String.IsNullOrWhiteSpace (Code)) { validationErrors.Add ("Code", "The code value cannot be null or empty."); }


            return validationErrors;

        }

        public override void MapDataFields (System.Data.DataRow currentRow) {
            
            base.MapDataFields (currentRow);

            Code = Data.Readers.DataReader.StringFromDatabase (currentRow, "Code");

            return;

        }

        public override Boolean Save () {

            Boolean success = false;

            try {

                Dictionary<String, String> validationErrors = Validate ();

                if (validationErrors.Count > 0) {

                    Application.SetLastException (new Exception ("Invalid [" + validationErrors.Keys.First () + "]: " + validationErrors.Values.First ()));

                    return false;

                }

                UpdateModifiedToSessionUser ();


                System.Data.IDbCommand command = Application.EnvironmentDatabase.CreateCommand (ObjectType + "_InsertUpdate");

                command.CommandType = System.Data.CommandType.StoredProcedure;


                Application.EnvironmentDatabase.AppendCommandParameter (command, "Id", Id);

                Application.EnvironmentDatabase.AppendCommandParameter (command, "Name", Name, 60);

                Application.EnvironmentDatabase.AppendCommandParameter (command, "Description", Description, 999);

                Application.EnvironmentDatabase.AppendCommandParameter (command, "Code", Code, 3);

                Application.EnvironmentDatabase.AppendCommandParameter (command, "Enabled", Enabled);

                Application.EnvironmentDatabase.AppendCommandParameter (command, "ModifiedId", Application.Session.UserAccountName, 60);

                success = (command.ExecuteNonQuery () > 0);

                if (success) { SetIdentity (); }

            }

            catch (Exception exception) {

                success = false;

                Application.SetLastException (exception);

            }

            return success;

        }

        public override Boolean Delete () {

            return base.DeleteFromDatabase (DeleteStatementById (), Application.EnvironmentDatabase);

        }

        #endregion 


    }

}
